//
//  SkuSelectView.m
//  JiaoX
//
//  Created by yinqy on 15/1/14.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "SkuSelectView.h"
#import "ProdSub.h"
#import "ProdSkuView.h"

#define CHILD_HEIGHT 35
@interface SkuSelectView()
{
    UIButton *_selectBtn;
    NSArray *_prodArray;
}

@end
@implementation SkuSelectView

-(instancetype)initWithProdSubArray:(NSArray *)array{
    self = [super init];
    if (self) {
        self.width = JXScreenW-SKU_MARGINLEFT;
        NSArray *resultArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
            return ((ProdSub *)obj1).prodBuyLink.length <((ProdSub *)obj2).prodBuyLink.length;
        }];
        _prodArray = resultArray;
        
        CGFloat paddingTop = JXPublicInset;
        CGFloat paddingLeft = JXPublicInset;
        CGFloat width = self.width;
        
        // 当前行数
        CGFloat currentRow = 0;
        // 当前行宽度偏移量
        CGFloat currentWidthOffset = 0;
        // 最小条目宽度
        CGFloat minItemWidth = (width-JXPublicInset*4)/3;
        
        for (int i =0; i<resultArray.count; i++) {
            
            ProdSub *sub = resultArray[i];
            
            UIButton *btn = [[UIButton alloc]init];
            [btn.titleLabel setFont:[UIFont systemFontOfSize:[DimensUtils getScaleX:15]]];
            [btn setTag:i];
            [btn setBackgroundImage:[UIImage imageWithColor:JXThemeColor] forState:UIControlStateSelected];
            [btn setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            
            NSString *text =sub.prodBuyLink;
            
            if(text.length>4 && text.length<=8){
                //大于4个字，小于8个字，需要两格
                if (currentWidthOffset > minItemWidth+JXPublicInset) {
                    // 当然偏移量大于第一个格子，需要换行
                    currentWidthOffset = 0;
                    currentRow++;
                }
            }else if (text.length > 8) {
                // 大于8个字，需要三格
                if (currentWidthOffset > 0) {
                    // 当然偏移量大于0，需要换行
                    currentWidthOffset = 0;
                    currentRow++;
                }
            }else{
                // 小于等于4个字，需要一格
                if (currentWidthOffset >= width - JXPublicInset) {
                    // 当然偏移量大于0，需要换行
                    currentWidthOffset = 0;
                    currentRow++;
                }
            }
            
            CGFloat btnX = paddingLeft +currentWidthOffset;
            CGFloat btnY = paddingTop + currentRow * CHILD_HEIGHT + currentRow * paddingTop;
            
            CGFloat btnW = minItemWidth;
            
            // 通过字数判断长度
            if (text.length > 4 && text.length <= 8) {
                // 大于4个字，小于8个字，需要两格
                btnW = minItemWidth * 2 + JXPublicInset;
            } else if (text.length > 8) {
                // 大于8个字，需要三格
                btnW = minItemWidth * 3 + JXPublicInset * 2;
            }
            
            currentWidthOffset = btnX+btnW;
            
            [btn setFrame:CGRectMake(btnX, btnY, btnW, CHILD_HEIGHT)];
            [btn setTitle:text forState:UIControlStateNormal];
            [btn setTitleColor:JXColor(77, 77, 77) forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
            
            [self addSubview:btn];
            
            if(resultArray.count==1){
                [btn setSelected:YES];
                _selectBtn = btn;
            }
            if (sub.isCanBuy == 0) {
                [btn setEnabled:NO];
                [btn setBackgroundImage:[UIImage imageWithColor:JXColor(204, 204, 204)] forState:UIControlStateDisabled];
            }
        }
        [self setBounds:CGRectMake(0, 0, width, CGRectGetMaxY(((UIView *)[[self subviews] lastObject]).frame)+JXPublicInset)];
    }
    return self;
}

-(BOOL)hasOneChecked{
    return _selectBtn!=nil;
}

-(void)btnClick:(UIButton *)btn{
    if(_selectBtn!=nil && _selectBtn.tag == btn.tag){
        return;
    }else{
        [btn setSelected:YES];
        [_selectBtn setSelected:NO];
        _selectBtn = btn;
    }
}
-(NSUInteger)getSelectIndex{
    return _selectBtn.tag;
}

-(ProdSub *)getSelectProdSub{
    return _prodArray[_selectBtn.tag];
}

-(void)clearSelected{
    if (_selectBtn) {
        [_selectBtn setSelected:NO];
        _selectBtn = nil;
    }
}


@end
